package com.read.scriptures.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.read.scriptures.R;
import com.read.scriptures.app.HuDongApplication;
import com.read.scriptures.config.PreferenceConfig;
import com.read.scriptures.config.SystemConfig;
import com.read.scriptures.constants.SystemConstants;
import com.read.scriptures.event.MEvent;
import com.read.scriptures.event.MEventKeys;
import com.read.scriptures.event.MEventManager;
import com.read.scriptures.model.Chapter;
import com.read.scriptures.util.SearchTextUtil;
import com.read.scriptures.util.SharedUtil;
import com.read.scriptures.widget.FlexiListView;
import com.read.scriptures.widget.sliding.SlidingAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

/**
 * Created by LGM.
 * Datetime: 2015/7/5.
 * Email: lgmshare@mgail.com
 */
public class ChapterReadSlidingAdapter extends SlidingAdapter<List<String>> implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private Context mCcontext;
    private List<Chapter> mChapters;
    private int mInitPageIndex = 0;
    private int mPageIndex = 0;
    private int mTipsPostion = 0;
    private String mTipsKeyword;
    private String mTipsContent;
    private int mSearchType;
    private String mChapterName;
    private String mChapterContent;
    private String mChapterNameKeyWord;
    public boolean mTipsValidate = true;
    private AdapterView.OnItemClickListener mOnItemClickListener;
    private AdapterView.OnItemLongClickListener mOnItemLongClickListener;
    //阅读模式
    private int mReadModel = 0;
    //文字大小
    private int mTextSize = 24;
    //简繁模式
    private int mTextModel = 0;
    //阅读背景色
    private int mBackgroudColor = 0;
    //阅读字段行间距
    private int mTextMagin = 0;

    private int mTextAround = 10;

    private int mTopAndBottomMargin = 0;

    private boolean isSJmode;

    private int mLineMargin = 0;

    private int HUAI_ZHU_CHAPTER = 0; //怀著标致
    private int HUAI_ZHU_CHAPTER_HAS_ZW = 0; //怀著标致中文  1
    //文字颜色
    private int mTextColor = Color.parseColor(SystemConfig.DEFAULT_READ_TEXT_COLOR_DEFAULT);

    //add
    private boolean isFirstRender = true;
    private String mfirstKeyword;
    private String mChapterContentNohtml;
    public ChapterReadSlidingAdapter(Context context, List<Chapter> chapterList, int pageIndex, boolean isSJmode, int flag, int flag1) {
        if (pageIndex >= chapterList.size()) {
            pageIndex = chapterList.size() - 1;
        }
        this.mCcontext = context;
        this.mChapters = chapterList;
        this.mPageIndex = chapterList.size() <= pageIndex ? 0 : pageIndex;
        this.mInitPageIndex = pageIndex;
        this.isSJmode = isSJmode;
        this.HUAI_ZHU_CHAPTER = flag;
        this.HUAI_ZHU_CHAPTER_HAS_ZW = flag1;
    }

    public void setChapters(List<Chapter> mChapters) {
        this.mChapters = mChapters;
    }

    public void setPageIndex(int pageIndex) {
        this.mPageIndex = pageIndex;
        this.mInitPageIndex = pageIndex;
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    public void setTipsPostion(int tipsPostion) {
        this.mTipsPostion = tipsPostion;
    }

    public void setTipsKeyword(String tipsKeyword) {
        this.mTipsKeyword = tipsKeyword;
    }

    public void setTipContent(String tipsContent) {
        this.mTipsContent = tipsContent;
    }

    public void setSearchType(int mSearchType) {
        this.mSearchType = mSearchType;
    }

    public void setChapterName(String mChapterName) {
        this.mChapterName = mChapterName;
    }

    public void setChapterContent(String chapterContent) {
        this.mChapterContent = chapterContent;
    }

    public void setChapterNameKeyWord(String mChapterNameKeyWord) {
        this.mChapterNameKeyWord = mChapterNameKeyWord;
    }

    public void setReadModel(int readModel) {
        this.mReadModel = readModel;
    }

    public void setTextSize(int textSize) {
        this.mTextSize = textSize;
    }

    public void setTextMargin(int textMargin) {
        this.mTextMagin = textMargin;
    }
//    public void setTopAndBottomMargin(int mTopAndBottomMargin) {
//        this.mTopAndBottomMargin = mTopAndBottomMargin;
//    }

    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    public void setTextAroundMargin(int textMargin) {
        this.mTextAround = textMargin;
    }

    public void setLineMargin(int mLineMargin) {
        this.mLineMargin = mLineMargin;
    }

    public void setTextModel(int textModel) {
        this.mTextModel = textModel;
    }

    public int getTextModel() {
        return mTextModel;
    }

    public void setBackgroudColor(int backgroudColor) {
        this.mBackgroudColor = backgroudColor;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }
//【bug1】发送监听
private void gethow(ListView listView) {
    listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // 移除布局监听器
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                listView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            int firstVisiblePos = listView.getFirstVisiblePosition();
            int lastVisiblePos = listView.getLastVisiblePosition();
            if (firstVisiblePos == AdapterView.INVALID_POSITION || lastVisiblePos == AdapterView.INVALID_POSITION) {
                return;
            }

            final int visibleItemCount = lastVisiblePos - firstVisiblePos + 1;
            final AtomicInteger drawnCount = new AtomicInteger(0);

            for (int i = 0; i < listView.getChildCount(); i++) {
                final View itemView = listView.getChildAt(i);
                if (itemView == null) continue;

                // 创建监听器并保存引用
                final ViewTreeObserver.OnDrawListener drawListener = new ViewTreeObserver.OnDrawListener() {
                    private boolean isRemoved = false;

                    @Override
                    public void onDraw() {
                        if (isRemoved) {
                            return;
                        }

                        int count = drawnCount.incrementAndGet();
                        if (count == visibleItemCount) {
                            Log.d("ItemDraw", "所有可见item绘制完成！");
                            MEvent ev=new MEvent(MEventKeys.CHAPTER_ADAPTER_BEGIN_SHOW,"");
                            MEventManager.getInstance().postEvent(ev);
                        }

                        // 标记为已移除并延迟执行移除操作
                        isRemoved = true;
                        final ViewTreeObserver.OnDrawListener listener = this; // 保存当前监听器引用
                        itemView.post(new Runnable() {
                            @Override
                            public void run() {
                                if (itemView.getViewTreeObserver().isAlive()) {
                                    // 使用保存的监听器引用
                                    itemView.getViewTreeObserver().removeOnDrawListener(listener);
                                }
                            }
                        });
                    }
                };

                itemView.getViewTreeObserver().addOnDrawListener(drawListener);
            }
        }
    });
}
    /**
     * 移除字符串中的所有HTML标签
     * @param html 包含HTML标签的字符串
     * @return 移除标签后的纯文本
     */
    private static String removeHtmlTags(String html) {
        if (html == null || html.isEmpty()) {
            return "";
        }
        // 使用正则表达式匹配并移除所有HTML标签
        Pattern htmlTagPattern = Pattern.compile("<[^>]*>");
        return htmlTagPattern.matcher(html).replaceAll("");
    }
//            找到位置【bug2】
    private void selectPosition(List<String> lists,FlexiListView listView){
        int spaceIndex = mTipsKeyword.indexOf(' ');
        // 判断是否存在空格
        if (spaceIndex != -1) {
            // 截取从0到空格索引的子字符串
            mfirstKeyword = mTipsKeyword.substring(0, spaceIndex);
        } else {
            mfirstKeyword = mTipsKeyword;
            System.out.println("字符串中没有空格");
        }
        mChapterContentNohtml=removeHtmlTags(mChapterContent);
//            找到位置
        for(int i=0;i<lists.size();i++){
            if (mChapterContentNohtml != null && mChapterContentNohtml.equals(lists.get(i))) {
                // 执行相关逻辑
                if(lists.get(i).contains(mfirstKeyword)){
                    listView.setSelection(i);
                    break;
                }};
        }
    }
    @Override
    public View getView(View contentView, List<String> lists) {
        FlexiListView listView;
        LinearLayout linearLayout;
        ChapterReadAdapter chapterReadAdapter;//===========================================================================
        if (contentView == null) {
            contentView = LayoutInflater.from(mCcontext).inflate(R.layout.adapter_chapter_read_sliding_item, null);
            chapterReadAdapter = new ChapterReadAdapter(mCcontext);
            chapterReadAdapter.setFlag(HUAI_ZHU_CHAPTER_HAS_ZW);
            listView = (FlexiListView) contentView.findViewById(R.id.sliding_listview);
            listView.setOnItemClickListener(this);
            listView.setOnItemLongClickListener(this);
            listView.setAdapter(chapterReadAdapter);
        } else {
            listView = (FlexiListView) contentView.findViewById(R.id.sliding_listview);
            chapterReadAdapter = (ChapterReadAdapter) listView.getAdapter();
        }

        int srollSetting = SharedUtil.getInt(PreferenceConfig.Preference_read_sroll_setting, 0);
        if (srollSetting == 0) {
            listView.setVerticalScrollbarPosition(1);
        } else {
            listView.setVerticalScrollbarPosition(2);
        }

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (onMoveEventlister != null) {
                    onMoveEventlister.onMove();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                long curtime = System.currentTimeMillis();
                if (curtime - saveTime < 20) {
                    return;
                }
                saveTime = curtime;

                if (onMoveEventlister != null) {
                    onMoveEventlister.onMove();
                }
            }
        });


//        LinearLayout.LayoutParams lp1 =new LinearLayout.LayoutParams( listView.getLayoutParams());
//        lp1.setMargins(0,mTopAndBottomMargin,0,mTopAndBottomMargin);
//        listView.setLayoutParams(lp1);
//        listView.setPadding(0,mTopAndBottomMargin,0,mTopAndBottomMargin);
//        linearLayout.setBackgroundColor(mBackgroudColor);

        chapterReadAdapter.setList(lists);
        chapterReadAdapter.setShengJing(isSJmode);
        chapterReadAdapter.setReadModel(mReadModel);
        chapterReadAdapter.setTextSize(mTextSize);
        chapterReadAdapter.setTextMargin(mTextMagin);
        chapterReadAdapter.setLineMargin(mLineMargin);
//        chapterReadAdapter.setTopAndBottomMargin(mTopAndBottomMargin);
        chapterReadAdapter.setTextAroundMargin(mTextAround);
        chapterReadAdapter.setTextColor(mTextColor);
        //关键字提示
        if (mPageIndex == mInitPageIndex && mTipsValidate) {
//            mTipsValidate = false;
            chapterReadAdapter.setTipsPostion(mTipsPostion);
            if (mTextModel == SystemConfig.TEXT_MODEL_NORMAL) {
                chapterReadAdapter.setTipsKeyword(mTipsKeyword);
                chapterReadAdapter.setChapterContent(mChapterContent);
            } else {
                chapterReadAdapter.setTipsKeyword(SearchTextUtil.jian2fan(mTipsKeyword));
                chapterReadAdapter.setChapterContent(SearchTextUtil.jian2fan(mChapterContent));
            }
            chapterReadAdapter.setSearchType(mSearchType);
            chapterReadAdapter.setChapterName(mChapterName);
            chapterReadAdapter.setChapterNameKeyWord(mChapterNameKeyWord);
            chapterReadAdapter.setTipsContent(mTipsContent);
            if (HUAI_ZHU_CHAPTER_HAS_ZW == 1) {
//                if (HuDongApplication.mVersions_HZ.size() == 2) {
//                    listView.setSelection(mTipsPostion);
//                } else {
//                    listView.setSelection(mTipsPostion / 2);
//                }
                listView.setSelection(mTipsPostion);
            } else {
                listView.setSelection(mTipsPostion/2);
            }
        } else {
            chapterReadAdapter.setTipsPostion(-1);
            chapterReadAdapter.setTipsKeyword(null);
            listView.setSelection(0);
        }
        chapterReadAdapter.setTextModel(getTextModel());
        chapterReadAdapter.notifyDataSetChanged();
        //首次渲染定位搜索位置

        gethow(listView);

        if(isFirstRender&& !TextUtils.isEmpty(mTipsKeyword)){
            if(isSJmode){
                int pos = mTipsPostion/5 + (HuDongApplication.mVersions.size()-1)*10;
                Log.e("tip","scrollpos"+pos);
                listView.setSelection(pos+1);
            }else{
                int pos = mTipsPostion + (HuDongApplication.mVersions_HZ.size()-1)*10;
                Log.e("tip","scrollpos"+pos);
                listView.setSelection(pos);
            }

            selectPosition(lists,listView);
//            for(int i=0;i<lists.size();i++){
//                if(lists.get(i).contains(mTipsKeyword)){
//                    listView.setSelection(i);
//                    break;
//                }
//            }
//            listView.setSelection(chapterReadAdapter.scrollPos);
            isFirstRender = false;
//            listView.scrollTo(0,mTipsPostion);
        }

        //阅读模式
        if (mReadModel == SystemConfig.READ_MODEL_NIGHT) {
            listView.setBackgroundColor(Color.parseColor(SystemConfig.DEFAULT_READ_BACKGROUND_NIGHT));
        } else {
            listView.setBackgroundColor(mBackgroudColor);
        }
        return contentView;
    }

    private long saveTime;

    public interface OnMoveEventlister {
        public void onMove();
    }

    public OnMoveEventlister getOnMoveEventlister() {
        return onMoveEventlister;
    }

    public ChapterReadSlidingAdapter setOnMoveEventlister(OnMoveEventlister onMoveEventlister) {
        this.onMoveEventlister = onMoveEventlister;
        return this;
    }

    private OnMoveEventlister onMoveEventlister;


    @Override
    public boolean hasNext() {
        return mPageIndex < mChapters.size() - 1;
    }

    @Override
    protected void computeNext() {
        ++mPageIndex;
    }

    @Override
    protected void computePrevious() {
        --mPageIndex;
    }

    @Override
    public boolean hasPrevious() {
        return mPageIndex > 0;
    }

    @Override
    public List<String> getPrevious() {
        return getContentList(mPageIndex - 1);
    }

    @Override
    public List<String> getNext() {
        return getContentList(mPageIndex + 1);
    }

    @Override
    public List<String> getCurrent() {
        return getContentList(mPageIndex);
    }

    private List<String> getContentList(int index) {
        List<String> content = null;
        if (mChapters != null) {
            if (index >= mChapters.size()) {
                index = mChapters.size() - 1;
            }
            if (HUAI_ZHU_CHAPTER_HAS_ZW == 1) {
                content = SearchTextUtil.queryChaptreContent(mCcontext, mChapters.get(index), mTextModel, HUAI_ZHU_CHAPTER_HAS_ZW);
            } else {
                content = SearchTextUtil.queryChaptreContent(mCcontext, mChapters.get(index), mTextModel);
            }
        }
        if (content == null) {
            content = new ArrayList<String>();
        }
        return content;
    }

    public ListView getCurrentListView() {
        View view = getCurrentView();
        ListView listView = (ListView) view.findViewById(R.id.sliding_listview);
        return listView;
    }

    public ChapterReadAdapter getCurrentChapterReadAdapter() {
        View view = getCurrentView();
        ListView listView = (ListView) view.findViewById(R.id.sliding_listview);
        ChapterReadAdapter adapter = (ChapterReadAdapter) listView.getAdapter();
        return adapter;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mOnItemClickListener.onItemClick(parent, view, position, id);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        mOnItemLongClickListener.onItemLongClick(parent, view, position, id);
        return false;
    }

    public void setShengJing(boolean isSJmode) {
    }


}